package com.stu.exercise.controller;

import com.stu.exercise.entity.R;
import com.stu.exercise.entity.Shopping;
import com.stu.exercise.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @RequestMapping("/list")
    public R<List<Shopping>> list(@RequestBody Map<String, Object> map){
        return shopService.getListShop(map.get("name").toString());
    }

    @RequestMapping("/add")
    public R add(@RequestBody Shopping shopping){
        boolean save = shopService.save(shopping);
        if (save){
            return R.success("添加成功", null);
        }
        return R.error(500, "添加失败");
    }

    @RequestMapping("/edit")
    public R edit(@RequestBody Shopping shopping){
        boolean update = shopService.updateById(shopping);
        if (update){
            return R.success("编辑成功", null);
        }
        return R.error(500, "编辑失败");
    }

    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        boolean remove = shopService.removeById(id);
        if (remove){
            return R.success("删除成功", null);
        }
        return R.error(500, "删除失败");
    }

    @RequestMapping("/storage")
    public R storage(@RequestBody List<Integer> ids){
        return shopService.batchStorage(ids);
    }

}
